﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.DuLieu;
using QldtSdh.Utils;

namespace QldtSdh.BLL
{
    public partial class BizNghienCuuSinh
    {
        /// <summary>
        /// Lấy danh sách học viên theo lớp
        /// </summary>
        /// <returns></returns>
        public static MySortableBindingList<NghienCuuSinh> GetList(string maKhoaHoc, string maNganh)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var ls = from e in db.NghienCuuSinhDbSet
                         select e;
                if (!string.IsNullOrEmpty(maKhoaHoc) && maKhoaHoc != "-1")
                    ls = from e in ls
                         where e.MaKhoaHocNCSGUID == maKhoaHoc
                         select e;
                if (!string.IsNullOrEmpty(maNganh) && maNganh != "-1")
                    ls = from e in ls
                         where e.MaNganhDaoTaoNCSGUID == maNganh
                         select e;
                var lsSort = new MySortableBindingList<NghienCuuSinh>(ls.OrderBy(e => e.Ten).ThenBy(e => e.Ten).ToList());
                return lsSort;
            }
        }
        public static NghienCuuSinh GetByID(string id)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.NghienCuuSinhDbSet.Where(e => e.MaNCSGUID == id).FirstOrDefault();
            }
        }

        public static CODE_RESULT_RETURN Add(NghienCuuSinh obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                obj.MaNCSGUID = Guid.NewGuid().ToString();
                db.NghienCuuSinhDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<NghienCuuSinh>(nhatKy, null, obj.MaNCSGUID, LOAIDOITUONG.NGHIÊN_CỨU_SINH, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<NghienCuuSinh>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(NghienCuuSinh obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _hp = db.NghienCuuSinhDbSet.Where(e => e.MaNCSGUID == obj.MaNCSGUID).FirstOrDefault();
                if (_hp == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<NghienCuuSinh>(nhatKy, _hp, obj.MaNCSGUID, LOAIDOITUONG.NGHIÊN_CỨU_SINH, LOAITHAOTAC.Chỉnh_sửa);

                _hp.HoDem = obj.HoDem;
                _hp.Ten = obj.Ten;
                _hp.GioiTinh = obj.GioiTinh;
                _hp.NgaySinh = obj.NgaySinh;
                _hp.NoiSinh = obj.NoiSinh;
                _hp.QuocTich = obj.QuocTich;
                _hp.SoDienThoai = obj.SoDienThoai;
                _hp.DiaChi = obj.DiaChi;
                _hp.Email = obj.Email;
                _hp.MaKhoaHocNCSGUID = obj.MaKhoaHocNCSGUID;
                _hp.MaNganhDaoTaoNCSGUID = obj.MaNganhDaoTaoNCSGUID;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<NghienCuuSinh>(nhatKy, _hp);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(string maNCS)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _hp = db.NghienCuuSinhDbSet.Where(e => e.MaNCSGUID == maNCS).FirstOrDefault();
                if (_hp == null)
                    return CODE_RESULT_RETURN.ThatBai;
                db.NghienCuuSinhDbSet.Remove(_hp);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<NghienCuuSinh>(nhatKy, _hp, _hp.MaNCSGUID, LOAIDOITUONG.NGHIÊN_CỨU_SINH, LOAITHAOTAC.Xóa);
                BizNhatKyHoatDong.Add<NghienCuuSinh>(nhatKy, null);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }
}
